<?php

declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/10/31
 * Time: 4:28 PM
 */

namespace App\Entity;

use App\Inc\OrderInc;
use App\Models\ProductSku;

class ProductSkuEntity extends ABaseEntity
{
    public function __construct(ProductSku $model = null)
    {
        if ($model === null) {
            $model = new ProductSku();
        }
        $this->model = $model;
    }

    /**
     * @return int
     */
    public static function getProductSkuIdByCityIdAndSkuTitle(string $title, int $cityId)
    {
        $entity = self::findByCityIdAndTitle($title, $cityId);
        if ($entity === null) {
            return 0;
        }

        return $entity->getId();
    }

    /**
     * @return self
     */
    public static function findByCityIdAndTitle(string $title, int $cityId)
    {
        $model = ProductSku::where('title', $title)
            ->where('city_id', $cityId)
            ->first();

        if ($model === null) {
            $model = ProductSku::where('title', $title)
                ->where('city_id', 0)
                ->first();
        }

        if ($model === null) {
            return null;
        }

        return new self($model);
    }

    /**
     * @return ProductSku
     */
    public static function getNewModelInstance()
    {
        $productSkuModel                        = new ProductSku();
        $productSkuModel->service_frequency     = 1;
        $productSkuModel->service_ordering_time = 0;
        $productSkuModel->purchase_product_id   = 0;
        $productSkuModel->sku_type              = OrderInc::SKU_TYPE_普通;

        return $productSkuModel;
    }

    /**
     * 新加购实例
     *
     * @return ProductSku
     */
    public static function getNewSupplementModelInstance()
    {
        $productSkuModel                        = new ProductSku();
        $productSkuModel->sku_show_type         = OrderInc::SKU_SHOW_TYPE_多规格;
        $productSkuModel->service_frequency     = 1;
        $productSkuModel->service_ordering_time = 0;
        $productSkuModel->coupon_useable        = 2;
        $productSkuModel->sku_type              = OrderInc::SKU_TYPE_加购;
        $productSkuModel->description           = '';

        return $productSkuModel;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->model->id;
    }

    /**
     * @return string
     */
    public function getTitle()
    {
        return$this->model->title;
    }

    public function setTitle(string $value)
    {
        $this->model->title = $value;
    }

    /**
     * @return int
     */
    public function getProductId()
    {
        return $this->model->product_id;
    }

    public function setProductId(int $value)
    {
        $this->model->product_id = $value;
    }

    /**
     * @return int
     */
    public function getCityId()
    {
        return $this->model->city_id;
    }

    /**
     * @return string
     */
    public function getPrice()
    {
        return $this->model->price;
    }

    public function setPrice(string $value)
    {
        $this->model->price = $value;
    }

    /**
     * @return string
     */
    public function getOldPrice()
    {
        return $this->model->old_price;
    }

    public function setOldPrice(string $value)
    {
        $this->model->old_price = $value;
    }

    /**
     * @return int
     */
    public function getProductUnitId()
    {
        return $this->model->product_unit_id;
    }

    public function setProductUnitId(int $value)
    {
        $this->model->product_unit_id = $value;
    }

    /**
     * 原来不可用是0 现在变为 2 不可用
     *
     * @return int
     */
    public function getCouponUseable()
    {
        if (in_array($this->model->coupon_useable, [0, 2])) {
            return 0;
        } else {
            return 1;
        }
    }

    public function setCouponUseable(int $value)
    {
        $this->model->coupon_useable = $value;
    }

    /**
     * @return string
     */
    public function getStopSale()
    {
        return $this->model->stop_sale;
    }

    /**
     * @return int
     */
    public function getBuyMinNum()
    {
        return $this->model->buy_min_num;
    }

    public function setBuyMinNum(int $value)
    {
        $this->model->buy_min_num = $value;
    }

    /**
     * @return int
     */
    public function getBuyMaxNum()
    {
        return $this->model->buy_max_num;
    }

    public function setBuyMaxNum(int $value)
    {
        $this->model->buy_max_num = $value;
    }

    /**
     * @return int
     */
    public function getSkuShowType()
    {
        return $this->model->sku_show_type;
    }

    public function setSkuShowType(int $val)
    {
        $this->model->sku_show_type = $val;
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return (string) $this->model->description;
    }

    public function setDescription(string $value)
    {
        $this->model->description = $value;
    }

    /**
     * @return int
     */
    public function getOnSale()
    {
        return $this->model->on_sale;
    }

    public function setOnSale(int $value)
    {
        $this->model->on_sale = $value;
    }

    public function setBeginSale(string $value)
    {
        $this->model->begin_sale = $value;
    }

    public function setStopSale(string $value)
    {
        $this->model->stop_sale = $value;
    }

    public function setOrderBy(int $value)
    {
        $this->model->order_by = $value;
    }
}
